Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ExcelUtil = undefined;
var BaseExcel = require("AA_BaseExcel");
var ExcelUtil = function () {
  function _creator() {}
  _creator.valueToString = function (t, o) {
    switch (t) {
      case "string":
        return o;
      case "string[]":
        return o.join(",");
      case "number":
        return o + "";
      case "number[]":
        return o.join(",");
      case "number[][]":
        var n = [];
        for (var i = 0; i < o.length; i++) {
          n.push(o[i].join(","));
        }
        return n.join(";");
      case "cc.Vec2":
        return o.x + "," + o.y;
      case "cc.Vec2[]":
        var a = [];
        for (i = 0; i < o.length; i++) {
          a.push(o[i].x + "," + o[i].y);
        }
        return a.join(";");
      case "cc.Color":
        return o.toHEX("#rrggbb");
      case "cc.Color[]":
        var r = [];
        for (i = 0; i < o.length; i++) {
          r.push(o[i].toHEX("#rrggbb"));
        }
        return r.join(",");
      case "itemData":
        return _creator.itemToStr([o]);
      case "itemData[]":
        var c = [];
        for (i = 0; i < o.length; i++) {
          c.push(_creator.itemToStr([o[i]]));
        }
        return c.join(";");
      case "boolean":
        return (o ? 1 : 0) + "";
      case "boolean[]":
        var s = [];
        for (i = 0; i < o.length; i++) {
          s.push(o[i] ? 1 : 0);
        }
        return s.join(",");
      default:
        return o;
    }
  };
  _creator.dataTypeConvert = function (t, o) {
    switch (o) {
      case "string":
        return t;
      case "string[]":
        return t.split(",");
      case "number":
        if ("" == t) {
          return 0;
        } else {
          return Number(t);
        }
      case "number[]":
        return _creator.stringToNumberArr(t);
      case "number[][]":
        return _creator.stringToNumberArrArr(t);
      case "cc.Vec2":
        return _creator.stringToVec(t);
      case "cc.Vec2[]":
        return _creator.stringToVecArr(t);
      case "cc.Color":
        return _creator.stringToColor(t);
      case "cc.Color[]":
        return _creator.stringToColorArr(t);
      case "itemData":
        return _creator.stringToItem(t);
      case "itemData[]":
        return _creator.stringToItemArr(t);
      case "boolean":
        return _creator.stringToBoolean(t);
      case "boolean[]":
        return _creator.stringTpBooleanArr(t);
      default:
        return t;
    }
  };
  _creator.itemToStr = function (e) {
    var t = "";
    for (var o = 0; o < e.length; o++) {
      t = 0 == o ? e[o].key + "," + e[o].value : t + ";" + e[o].key + "," + e[o].value;
    }
    return t;
  };
  _creator.stringToBoolean = function (e) {
    return !("" == e || "0" == e || "false" == e.toLowerCase());
  };
  _creator.stringTpBooleanArr = function (t) {
    var o = [];
    if (t.length > 0) {
      var n = t.split(",");
      for (var i = 0; i < n.length; i++) {
        o.push(_creator.stringToBoolean(n[i]));
      }
    }
    return o;
  };
  _creator.stringToColorArr = function (t) {
    var o = [];
    if (t.length > 0) {
      var n = t.split(",");
      for (var i = 0; i < n.length; i++) {
        o.push(_creator.stringToColor(n[i]));
      }
    }
    return o;
  };
  _creator.stringToColor = function (e) {
    if (e.length >= 6) {
      var t = "0x" + e.substring(0, 2);
      var o = "0x" + e.substring(2, 4);
      var n = "0x" + e.substring(4, 6);
      var i = "";
      e.length > 6 && (i = "0x" + e.substring(6, 8));
      var a = parseInt(t);
      var r = parseInt(o);
      var c = parseInt(n);
      var s = "" == i ? 255 : parseInt(i);
      return new cc.Color(a, r, c, s);
    }
    return cc.Color.BLACK;
  };
  _creator.stringToVecArr = function (t) {
    var o = [];
    if (t.length > 0) {
      var n = t.split(";");
      for (var i = 0; i < n.length; i++) {
        o.push(_creator.stringToVec(n[i]));
      }
    }
    return o;
  };
  _creator.stringToVec = function (t) {
    var o = _creator.stringToNumberArr(t);
    if (o.length >= 2) {
      return cc.v2(o[0], o[1]);
    } else {
      return cc.v2(0, 0);
    }
  };
  _creator.stringToNumberArr = function (e) {
    var t = [];
    if (e.length > 0) {
      var o = e.split(",");
      for (var n = 0; n < o.length; n++) {
        t.push(Number(o[n]));
      }
    }
    return t;
  };
  _creator.stringToNumberArrArr = function (t) {
    var o = [];
    if (t.length > 0) {
      var n = t.split(";");
      for (var i = 0; i < n.length; i++) {
        o.push(_creator.stringToNumberArr(n[i]));
      }
    }
    return o;
  };
  _creator.itemToString = function (e) {
    if (e.extent) {
      return e.key + "," + e.extent + "," + e.value;
    } else {
      return e.key + "," + e.value;
    }
  };
  _creator.stringToItem = function (t) {
    var o = _creator.stringToNumberArr(t);
    var i = o.length;
    if (2 == i) {
      return new BaseExcel.itemData(o[0], o[1]);
    } else if (i > 2) {
      return new BaseExcel.itemData(o[0], o[1], o[2]);
    } else {
      return null;
    }
  };
  _creator.stringToItemArr = function (t) {
    var o = [];
    if (t.length > 0) {
      var n = t.split(";");
      for (var i = 0; i < n.length; i++) {
        o.push(_creator.stringToItem(n[i]));
      }
    }
    return o;
  };
  _creator.itemArrToString = function (e) {
    var t = [];
    for (var o = 0; o < e.length; o++) {
      t.push(this.itemToString(e[o]));
    }
    return t.join(";");
  };
  _creator.jsonToValue = function (t, o, n, i) {
    var a = {
      talk_task: "talk"
    }[n];
    for (var r in o) {
      var c = t[r] || "";
      a && o[r] == a && c.indexOf("\\n") >= 0 && c.replace(/\\n/g, "\n");
      i[o[r]] = _creator.dataTypeConvert(c, i.typeStr[o[r]]);
    }
  };
  _creator.convert = function (t, o, n) {
    if (o instanceof cc.JsonAsset) {
      var i = o.json.rows;
      var a = o.json.header;
      var r = new Map();
      for (var c in i) {
        var s = new n();
        _creator.jsonToValue(i[c], a, t, s);
        h = parseInt(c);
        if (h) {
          r.set(h, s);
        }
      }
      return r;
    }
    if (o instanceof Array) {
      r = new Map();
      a = [];
      i = {};
      for (var u = 0; u < o.length; u++) {
        var l = [];
        if (0 == u) {
          for (var c in o[u]) a.push(c);
        }
        var d = true;
        for (var c in o[u]) if (0 == c.length || o[u][c].indexOf("##1") >= 0 || o[u][c].indexOf("##2") >= 0) {
          d = false;
          break;
        }
        if (d) {
          var p = 0;
          var h = undefined;
          for (var c in o[u]) {
            0 == p && (h = o[u][c]);
            l.push(o[u][c]);
            p++;
          }
          h && h.length > 0 && (i[h] = l);
        }
      }
      for (var c in i) {
        s = new n();
        _creator.jsonToValue(i[c], a, t, s);
        h = parseInt(c);
        if (h) {
          r.set(h, s);
        }
      }
      return r;
    }
  };
  _creator.excelToArray = function (e, t) {
    t = t || ",";
    var o = new RegExp("(\\" + t + '|\\r?\\n|\\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^"\\' + t + "\\r\\n]*))", "gi");
    var n = [[]];
    for (var i = null; i = o.exec(e);) {
      var a;
      var r = i[1];
      r.length && r != t && n.push([]);
      a = i[2] ? i[2].replace(new RegExp('""', "g"), '"') : i[3];
      n[n.length - 1].push(a);
    }
    n.splice(-1);
    return n;
  };
  _creator.excelToObject = function (t) {
    var o = _creator.excelToArray(t);
    var n = [];
    for (var i = 1; i < o.length; i++) {
      n[i - 1] = {};
      for (var a = 0; a < o[0].length && a < o[i].length; a++) {
        var r = o[0][a];
        n[i - 1][r] = o[i][a];
      }
    }
    return n;
  };
  _creator.excelToJson = function (t) {
    return JSON.stringify(_creator.excelToObject(t));
  };
  _creator.getSameData = function (e, t, o, n, i) {
    var a = [];
    e.forEach(function (e) {
      if (e[t] && e[t] == o) {
        if (n) {
          e[n] == i && a.push(e);
        } else {
          a.push(e);
        }
      }
    });
    return a;
  };
  return _creator;
}();
exports.ExcelUtil = ExcelUtil;